<template>
  <div>
    <el-dialog :title="ruleForm.id == '' ? '添加学生信息' : '编辑学生信息'" :visible.sync="addEditIsDialog_c" @close="resetForm('formName')" width="800px">
      <el-form :model="ruleForm" :rules="rules" ref="formName" label-width="120px" class="demo-ruleForm" :class="{ 'form-rules-show': seeType }">
        <el-row>
          <el-col :span="12">
            <el-form-item label="考场名称：" prop="examinationRoomId">
              <el-select v-model="ruleForm.examinationRoomId" placeholder="请选择考场名称" clearable @change="schoolChange" :disabled="seeType || userType != 0">
                <el-option v-for="item in schoolArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="学院名称：" prop="collegeId">
              <el-select v-model="ruleForm.collegeId" placeholder="请选择学院名称" @change="collegeChange" clearable :disabled="seeType">
                <el-option v-for="item in collegeArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="院系名称：" prop="facultyId">
              <el-select v-model="ruleForm.facultyId" placeholder="请选择院系名称" @change="facultyChange" clearable :disabled="seeType">
                <el-option v-for="item in facultyArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="专业名称：" prop="majorId">
              <el-select v-model="ruleForm.majorId" placeholder="请选择专业名称" @change="majorChange" clearable :disabled="seeType">
                <el-option v-for="item in majorArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="班级名称：" prop="classId">
              <el-select v-model="ruleForm.classId" placeholder="请选择班级名称" clearable :disabled="seeType">
                <el-option v-for="item in classArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="头像：" prop="headImage">
              <UploadImg para="image" @setImg="setImg" :imgUrl="ruleForm.headImage" width="100" height="100"></UploadImg>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="学号：" prop="code">
              <el-input v-model="ruleForm.code" placeholder="请输入学号" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="姓名：" prop="realName">
              <el-input v-model="ruleForm.realName" placeholder="请输入姓名" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="性别：" prop="sex">
              <el-select v-model="ruleForm.sex" placeholder="请选择性别" clearable :disabled="seeType">
                <el-option label="男" value="0"></el-option>
                <el-option label="女" value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="定位标签：" prop="labelId">
              <el-select v-model="ruleForm.labelId" placeholder="请选择定位标签" clearable :disabled="seeType">
                <el-option v-for="item in labelArray" :key="item.id" :label="item.labelName" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="手机号：" prop="phone">
              <el-input v-model="ruleForm.phone" placeholder="请输入手机号" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱：" prop="email">
              <el-input v-model="ruleForm.email" placeholder="请输入邮箱" clearable maxlength="100" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">取 消</el-button>
        <el-button type="primary" @click="submitForm('formName')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import UploadImg from '@/components/UploadImg.vue'

import { getStudentInfo, saveStudentInfo } from '@/api/user'
import { getLocationLabels } from '@/api/personLocation'

import { getSchoolInfos, getMajorInfos, getClassInfos } from '@/api/school'

export default {
  components: {
    UploadImg
  },
  props: {
    addEditId: {
      type: String,
      default: ''
    },
    addEditStatus: {
      type: String,
      default: 'add'
    },
    addEditIsDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        id: '',
        examinationRoomId: '',
        collegeId: '',
        facultyId: '',
        majorId: '',
        classId: '',
        headImage: '',
        code: '',
        realName: '',
        sex: '',
        phone: '',
        email: '',
        labelId: ''
      },
      rules: {
        // 表单规则
        examinationRoomId: [{ required: true, message: '请选择考场名称', trigger: 'change' }],
        collegeId: [{ required: true, message: '请选择学院名称', trigger: 'change' }],
        facultyId: [{ required: true, message: '请选择院系名称', trigger: 'change' }],
        majorId: [{ required: true, message: '请选择专业名称', trigger: 'change' }],
        classId: [{ required: true, message: '请选择班级名称', trigger: 'change' }],
        headImage: [{ required: true, message: '请上传头像', trigger: 'change' }],
        code: [{ required: true, message: '请输入学号', trigger: 'blur' }],
        realName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        sex: [{ required: true, message: '请选择性别', trigger: 'blur' }],
        labelId: [{ required: true, message: '请选择定位标签', trigger: 'blur' }],
        phone: [{ required: false, message: '请输入手机号', trigger: 'blur' }],
        email: [{ required: false, message: '请输入邮箱', trigger: 'blur' }]
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
      schoolArray: [], // 考场集合
      collegeArray: [], // 学院集合
      facultyArray: [], // 院系集合
      majorArray: [], // 专业集合
      classArray: [], // 班级集合
      labelArray: [], // 标签集合
      userType: '', // 用户类型（0超级管理员，1考场管理员）
      examinationRoomId: '' // 考场id
    }
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val
      if (this.addEditIsDialog_c) {
        this.userType = Number(window.localStorage.getItem('userType'))
        this.examinationRoomId = Number(window.localStorage.getItem('examinationRoomId'))
        if (this.userType == 1) {
          this.ruleForm.examinationRoomId = this.examinationRoomId

          if (this.addEditStatus == 'add') {
            // 获取学院列表
            this.getCollegeList()

            // 获取院系列表
            this.getFacultyList()

            // 获取专业列表
            this.getMajorList()

            // 获取班级列表
            this.getClassList()

            // 获取定位标签列表
            this.getLocationLabels()
          }
        }

        // 添加
        if (this.addEditStatus == 'add') {
          this.ruleForm.id = ''
          this.seeType = false
        }

        // 编辑
        if (this.addEditStatus == 'edit') {
          this.ruleForm.id = this.addEditId
          this.seeType = false
          this.getDetail()
        }

        // 查看
        if (this.addEditStatus == 'see') {
          this.ruleForm.id = this.addEditId
          this.seeType = true
          this.getDetail()
        }

        // 获取考场列表
        this.getSchoolList()
      }
    }
  },
  methods: {
    // 详情
    getDetail() {
      getStudentInfo({ id: this.addEditId }).then((res) => {
        if (res.code == 200) {
          res.data.sex = res.data.sex != undefined ? res.data.sex.toString() : ''
          this.ruleForm = res.data

          // 获取学院列表
          this.getCollegeList()

          // 获取院系列表
          this.getFacultyList()

          // 获取专业列表
          this.getMajorList()

          // 获取班级列表
          this.getClassList()

          // 获取定位标签列表
          this.getLocationLabels()
        }
      })
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          saveStudentInfo(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: res.msg
              })

              // 关闭弹窗
              this.$parent.closeAddEdit()
              // 刷新列表
              this.$parent.onSubmit()
            }
          })
        }
      })
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.$parent.closeAddEdit()
    },
    // 获取考场列表
    getSchoolList() {
      getSchoolInfos().then((res) => {
        if (res.code == 200) {
          this.schoolArray = res.data
        }
      })
    },
    // 考场下拉切换
    schoolChange() {
      this.ruleForm.collegeId = ''
      this.collegeArray = []

      this.ruleForm.facultyId = ''
      this.facultyArray = []

      this.ruleForm.majorId = ''
      this.majorArray = []

      this.ruleForm.classId = ''
      this.classArray = []

      this.ruleForm.labelId = ''
      this.labelArray = []

      // 获取学院列表
      this.getCollegeList()

      // 获取定位标签列表
      this.getLocationLabels()
    },
    // 获取学院列表
    getCollegeList() {
      if (!this.ruleForm.examinationRoomId || this.ruleForm.examinationRoomId == '') {
        return
      }

      getCollegeInfos({ id: this.ruleForm.examinationRoomId }).then((res) => {
        if (res.code == 200) {
          this.collegeArray = res.data
        }
      })
    },
    // 学院下拉切换
    collegeChange() {
      this.ruleForm.facultyId = ''
      this.facultyArray = []

      this.ruleForm.majorId = ''
      this.majorArray = []

      this.ruleForm.classId = ''
      this.classArray = []

      // 获取院系列表
      this.getFacultyList()

      // 获取班级列表
      this.getClassList()
    },
    // 获取院系列表
    getFacultyList() {
      if (!this.ruleForm.collegeId || this.ruleForm.collegeId == '') {
        return
      }

      getFacultyInfos({ id: this.ruleForm.collegeId }).then((res) => {
        if (res.code == 200) {
          this.facultyArray = res.data
        }
      })
    },
    // 院系下拉切换
    facultyChange() {
      this.ruleForm.majorId = ''
      this.majorArray = []

      this.ruleForm.classId = ''
      this.classArray = []

      // 获取专业列表
      this.getMajorList()
    },
    // 获取专业列表
    getMajorList() {
      if (!this.ruleForm.facultyId || this.ruleForm.facultyId == '') {
        return
      }

      getMajorInfos({ id: this.ruleForm.facultyId }).then((res) => {
        if (res.code == 200) {
          this.majorArray = res.data
        }
      })
    },
    // 专业下拉切换
    majorChange() {
      this.ruleForm.classId = ''
      this.classArray = []

      // 获取班级列表
      this.getClassList()
    },
    // 获取班级列表
    getClassList() {
      if (!this.ruleForm.majorId || this.ruleForm.majorId == '') {
        return
      }

      getClassInfos({ id: this.ruleForm.majorId }).then((res) => {
        if (res.code == 200) {
          this.classArray = res.data
        }
      })
    },
    // 获取定位标签列表
    getLocationLabels() {
      if (!this.ruleForm.examinationRoomId || this.ruleForm.examinationRoomId == '') {
        return
      }

      getLocationLabels({ id: this.ruleForm.examinationRoomId }).then((res) => {
        if (res.code == 200) {
          this.labelArray = res.data
        }
      })
    },
    // 上传图片
    setImg(url) {
      this.ruleForm.headImage = url
    }
  }
}
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;

  .el-input-number {
    width: 100%;
  }
}
</style>
